#include "Game.h"

IGame* createGame( HINSTANCE hInst, HWND hWnd )
{
	return new Game( hInst, hWnd );
}

Game::Game( HINSTANCE hInst, HWND hWnd ) :
	m_input( hInst, hWnd )
{
	// initialize camera
	m_camera.hand = Camera::RIGHTHANDED;

	m_camera.eye = float3( 0, 0, 100.f );
	m_camera.at  = float3( 0, 0, 0 );
	m_camera.up  = float3( 0, 1, 0 );

	m_camera.projectionType = Camera::PERSPECTIVE_FOV;
	m_camera.perspectiveFov.fovy = D3DXToRadian(45.f);
	m_camera.perspectiveFov.aspect = 1024.f/768.f;

	m_camera.zn = 1.0f;
	m_camera.zf = 1000.0f;

	// rigid polys
	m_polys.resize( 100 );

	// add rp's to dynamic system and collision constraint
	for( int i = 0; i < m_polys.size(); i++ )
	{
		m_dynamics.dynObjects.insert( & m_polys[i] );
		m_polycollision.bodies.insert( & m_polys[i] );
	}

	// add collision constraint to simulation
	m_dynamics.constraints.insert( &m_polycollision );

	// setup polygon and color
	m_polygon.push_back( float3( cos(0.f), sin(0.f), 0 ) );
	m_polygon.push_back( float3( -0.5f, +sqrt(3.f)/2, 0 ) );
	m_polygon.push_back( float3( -0.5f, -sqrt(3.f)/2, 0 ) );

	m_color = float4( 1.f, 1.f, 1.f, 1.f );
}

Game::~Game()
{
	m_dynamics.dynObjects.clear();
	m_polycollision.bodies.clear();
	m_polys.clear();
	m_d3dDevice = 0;
}

void Game::onCreateGraphics( D3DDevice device )
{
	m_d3dDevice = device;

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 world = identity44();
	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_WORLD, &world );
	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );

	// setup all dynamic objects using same polygon and color
	for( int i = 0; i < m_polys.size(); i++ )
	{
		m_polys[i].setup( device, m_polygon, m_color );
		m_polys[i].rigidBody.pos = float3( rand()%33-16, rand()%33-16, 0 );
		m_polys[i].rigidBody.vel = float3( rand()%5-2, rand()%5-2, 0 );
	}
}

void Game::onResetGraphics( D3DDevice device )
{
	//TODO: Add code to handle lost device ( resizing issue )

	m_d3dDevice = device;

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );

	// setup all dynamic objects using same polygon and color
	for( int i = 0; i < m_polys.size(); i++ )
	{
		m_polys[i].setup( device, m_polygon, m_color );
	}
}

void Game::onLostGraphics()
{
	m_dynamics.dynObjects.clear();
	m_polycollision.bodies.clear();
	m_polys.clear();
	m_d3dDevice = 0;
}

void Game::onDestroyGraphics()
{
	m_dynamics.dynObjects.clear();
	m_polycollision.bodies.clear();
	m_polys.clear();
	m_d3dDevice = 0;
}

void Game::onFrameMove(float dt)
{
	dt = 1/50.f;
	m_dynamics.step(dt);
}

void Game::onFrameRender(float dt)
{
	float44 world = identity44();
	m_dynamics.render(dt);
}
